package com.bruce.spring.http.service.controller;

import com.bruce.spring.http.api.OrderClient;
import com.bruce.spring.http.api.Page;
import com.bruce.spring.http.api.Store;
import com.bruce.spring.http.service.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Set;

/**
 * Created by bruce in 2025/6/17 02:15
 */
@RestController
@RequestMapping("/order")
public class OrderClientController implements OrderClient {
    private static final Logger log = LoggerFactory.getLogger(OrderClientController.class);

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public Page<Store> putJsonStores(Store store) {
        // 获取HttpServletRequest对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();

        log.info("postJsonStores requestURI:{}, queryString:{}", requestURI, queryString);
        log.info("postJsonStores requestURI:{}, getParameterMap:{}", requestURI, JsonUtil.toJson(request.getParameterMap()));

        store.setCreateTime(LocalDateTime.now());

        Page<Store> page = new Page<>();
        page.setData(store);

        log.info("postJsonStores: pageable:{}", JsonUtil.toJson(page));

        return page;
    }

    @Override
    public Page<Store> putFormStores(Store store) {
        // 获取HttpServletRequest对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();

        log.info("postFromStores requestURI:{}, queryString:{}", requestURI, queryString);
        log.info("postFromStores requestURI:{}, getParameterMap:{}", requestURI, JsonUtil.toJson(request.getParameterMap()));

        store.setCreateTime(LocalDateTime.now());

        Page<Store> page = new Page<>();
        page.setData(store);

        log.info("postFromStores: pageable:{}", JsonUtil.toJson(page));

        return page;
    }

    @Override
    public Store requestMap(Map<String, String> params) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        Map<String, String[]> parameterMap = request.getParameterMap();

        try {
            log.info("requestMap: params:{}, HttpServletRequest parameterMap:{}", params, objectMapper.writeValueAsString(parameterMap));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return new Store("map param", params.toString(), LocalDateTime.now());
    }

    @Override
    public Map<String, String> requestMapBody(Map<String, String> params) {
        log.info("requestMapBody: params:{}", params);
        return params;
    }

    @Override
    public ResponseEntity<Store> responseEntity(Map<String, String> params) {
        Store store = new Store(params.get("name"), params.toString(), LocalDateTime.now());
        if ("500".equals(params.get("code"))) {
            return ResponseEntity.status(500).body(store);
        }
        return ResponseEntity.ok(store);
    }

    @Override
    public Store usePathVars(Long storeId, Store store) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String requestURI = request.getRequestURI();
        log.info("storeId:{}, requestURI:{}", storeId, requestURI);
        return store;
    }

    @Override
    public Set<String> keySet(Long secretId, Long version) {
        return Set.of("aa","哈哈");
    }


}
